package com.yiwei.zxp.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.QualificationIdentityActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.event.SettingShopEvent;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.view.SureIndentityDilaog;
import com.yiwei.zxp.view.SureWithoutIndentityDilaog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-7-2.
 */

public class WithoutLicenseIdentityFragment extends BaseFragment {
    @BindView(R.id.img_idcard)
    ImageView imgIdcard;
    @BindView(R.id.btn_idcard_add)
    LinearLayout btnIdcardAdd;
    @BindView(R.id.btn_retake_idcard)
    TextView btnRetakeIdcard;
    @BindView(R.id.img_idcard_back)
    ImageView imgIdcardBack;
    @BindView(R.id.btn_idcard_add_back)
    LinearLayout btnIdcardAddBack;
    @BindView(R.id.btn_retake_idcard_back)
    TextView btnRetakeIdcardBack;
    @BindView(R.id.edit_realname)
    EditText editRealname;
    @BindView(R.id.btn_edit_realname)
    TextView btnEditRealname;
    @BindView(R.id.txt_idcard_no)
    TextView txtIdcardNo;
    @BindView(R.id.layout_idcard_identity)
    LinearLayout layoutIdcardIdentity;
    @BindView(R.id.img_idcard_hand)
    ImageView imgIdcardHand;
    @BindView(R.id.btn_idcard_add_hand)
    LinearLayout btnIdcardAddHand;
    @BindView(R.id.btn_retake_idcard_hand)
    TextView btnRetakeIdcardHand;
    Unbinder unbinder;

    public String idcard_front_img;
    public String idcard_no;
    public String realname;
    public String idcard_back_img;
    public String idcard_hand_img;
    private SureWithoutIndentityDilaog dialog;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_without_bussiness_identity;
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            idcard_no= getArguments().getString(Const.SettingShopData.Idcard.idcard_no);
            realname= getArguments().getString(Const.SettingShopData.Idcard.realname);
            idcard_front_img= getArguments().getString(Const.SettingShopData.Idcard.idcard_front_img);
            idcard_back_img= getArguments().getString(Const.SettingShopData.Idcard.idcard_back_img);
            idcard_hand_img= getArguments().getString(Const.SettingShopData.Idcard.idcard_hand_img);

            if (!TextUtils.isEmpty(idcard_no)) {
                layoutIdcardIdentity.setVisibility(View.VISIBLE);
                txtIdcardNo.setText(idcard_no);
            }
            if (!TextUtils.isEmpty(realname)) {
                editRealname.setText(realname);
            }

            if (!TextUtils.isEmpty(idcard_front_img)) {
                Glide.with(getContext()).load(idcard_front_img).into(imgIdcard);
                dismissFront();
            }
            if (!TextUtils.isEmpty(idcard_back_img)) {
                Glide.with(getContext()).load(idcard_back_img).into(imgIdcardBack);
                dismissBack();
            }
            if (!TextUtils.isEmpty(idcard_hand_img)) {
                Glide.with(getContext()).load(idcard_hand_img).into(imgIdcardHand);
                dismissHand();
            }
        }


        dialog = new SureWithoutIndentityDilaog(getContext()).setOnChooseListener(new SureWithoutIndentityDilaog.OnChooseListener() {
            @Override
            public void onSuccess() {
                SettingShopEvent event = new SettingShopEvent();
                event.event_type = Const.SettingShopType.Qualification;
                event.type = "2";
                event.idcard_front_img = idcard_front_img;
                event.idcard_no = idcard_no;
                event.realname = realname;
                event.idcard_back_img = idcard_back_img;
                event.idcard_hand_img = idcard_hand_img;
                EventBus.getDefault().post(event);
                getActivity().finish();
            }
        });

    }

    public void setData(String idcard_front_img, String idcard_no, String realname, String idcard_back_img, String idcard_hand_img) {
        this.idcard_front_img = idcard_front_img;
        this.idcard_no = idcard_no;
        this.realname = realname;
        this.idcard_back_img = idcard_back_img;
        this.idcard_hand_img = idcard_hand_img;


    }

    public void showSubmitDialog() {
        if (TextUtils.isEmpty(idcard_front_img)) {
            ToastUtil.makeText(getContext(), "身份证正面照片不能为空");
            return;
        }
        if (TextUtils.isEmpty(idcard_back_img)) {
            ToastUtil.makeText(getContext(), "身份证反面照片不能为空");
            return;
        }
        if (TextUtils.isEmpty(idcard_hand_img)) {
            ToastUtil.makeText(getContext(), "手持身份证照片不能为空");
            return;
        }
        if (TextUtils.isEmpty(UI.toString(txtIdcardNo))) {
            ToastUtil.makeText(getContext(), "身份证不能为空");
            return;
        }
        if (TextUtils.isEmpty(UI.toString(editRealname))) {
            ToastUtil.makeText(getContext(), "真实姓名不能为空");
            return;
        }

        idcard_no = UI.toString(txtIdcardNo);
        realname = UI.toString(editRealname);

        dialog.setData(idcard_no, realname);
        dialog.show();
    }


    public void setIdcardImg(Bitmap url) {
        Glide.with(getContext()).load(url).into(imgIdcard);
        dismissFront();
    }

    public void setIdcardImg(String url, String idcard_no, String realname) {
        idcard_front_img = url;
        Glide.with(getContext()).load(idcard_front_img).into(imgIdcard);
        layoutIdcardIdentity.setVisibility(View.VISIBLE);
        txtIdcardNo.setText(idcard_no);
        editRealname.setText(realname);
        dismissFront();
    }

    public void setIdcardBackImg(Bitmap url) {
        Glide.with(getContext()).load(url).into(imgIdcardBack);
        dismissBack();
    }

    public void setIdcardBackImg(String url) {
        idcard_back_img = url;
        Glide.with(getContext()).load(idcard_back_img).into(imgIdcardBack);
        dismissBack();
    }

    public void setHandIdcard(String url) {
        idcard_hand_img = url;
        Glide.with(getContext()).load(idcard_hand_img).into(imgIdcardHand);
        dismissHand();
    }

    private void dismissFront() {
        btnIdcardAdd.setVisibility(View.GONE);
        btnRetakeIdcard.setVisibility(View.VISIBLE);
    }

    private void dismissBack() {
        btnIdcardAddBack.setVisibility(View.GONE);
        btnRetakeIdcardBack.setVisibility(View.VISIBLE);
    }

    private void dismissHand() {
        btnIdcardAddHand.setVisibility(View.GONE);
        btnRetakeIdcardHand.setVisibility(View.VISIBLE);
    }

    public void setFocus(EditText edittext) {

        edittext.setFocusable(true);
        edittext.setFocusableInTouchMode(true);
        edittext.requestFocus();
        edittext.findFocus();
        InputMethodManager inputManager = (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(edittext, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_idcard_add, R.id.btn_retake_idcard, R.id.btn_idcard_add_back, R.id.btn_retake_idcard_back, R.id.btn_edit_realname, R.id.btn_idcard_add_hand, R.id.btn_retake_idcard_hand})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_idcard_add:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(1);
                break;
            case R.id.btn_retake_idcard:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(1);
                break;
            case R.id.btn_idcard_add_back:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(2);
                break;
            case R.id.btn_retake_idcard_back:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(2);
                break;
            case R.id.btn_edit_realname:
                setFocus(editRealname);
                break;
            case R.id.btn_idcard_add_hand:
                Dialog.showIdcardHandTips(getContext(), new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        ((QualificationIdentityActivity) getActivity()).startTakePhoto(4);
                    }

                    @Override
                    public void cancel() {

                    }
                });
                break;
            case R.id.btn_retake_idcard_hand:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(4);
                break;
        }
    }
}
